<template>
  <div class="app-container">
    <div class="filter-container">
      <center>
        <el-form>
          <el-form-item>
            <el-input v-model="search" placeholder="请输入搜索设备内容" style='width: 50%;'></el-input>
            <el-button type="primary" icon="el-icon-search" circle style='margin-left:10px;' @click="lists()"></el-button>
          </el-form-item>
        </el-form>
      </center>
    </div>
<!-- 这是一个 表格 -->
    <el-table
      :data="lists" v-loading.body="listLoading" element-loading-text="拼命加载中" border fit highlight-current-row
      style="width: 97.5%;margin:20px;margin-left:20px;">      
      <el-table-column align="center" prop="devId" label="设备编号" width="140">
      </el-table-column>
      <el-table-column align="center" prop="devModel" label="设备型号" width="150">
      </el-table-column>
      <el-table-column align="center" prop="devName" label="设备名称" width="150">
      </el-table-column>
      <el-table-column align="center" prop="devBrand" label="设备品牌" width="150">
      </el-table-column>
      <el-table-column align="center" prop="devType" label="类型/属性" width="150">
      </el-table-column>
      <el-table-column align="center" prop="devPosition" label="使用位置" width="150">
      </el-table-column>
      <el-table-column align="center" prop="devSituation" label="设备情况" width="150">
      </el-table-column>
      <el-table-column align="center" prop="createTime" label="创建时间" width="190">
      </el-table-column>
      <el-table-column align="center" prop="userName" label="维修人员" width="150">
      </el-table-column>
      <el-table-column align="center" prop="devRemark" label="备注" width="140">
      </el-table-column>
      <el-table-column align="center" label="操作" v-if="hasPerm('devices:update')">
        <template slot-scope="scope">
          <el-button align="center" type="success" size="small" v-if="hasPerm('devices:update')" @click="showUpdate(scope.$index)">编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

<!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-size="listQuery.pageRow"
      :total="totalCount"
      :page-sizes="[10, 20, 50, 100]"
      layout="sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- 这是一个button 点击弹框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form class="small-space" :model="tempDevice" label-position="left" label-width="100px"
               style='width: 600px; margin-left:50px;'>        
      <el-form-item label="设备编号" required>
          <el-input type="text" v-model="tempDevice.devId" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="设备型号" required>
          <el-input type="text" v-model="tempDevice.devModel" style="width: 250px;">
          </el-input> 
        </el-form-item>
        <el-form-item label="设备名称" required>
          <el-input type="text" v-model="tempDevice.devName" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="设备品牌" required>
          <el-input type="text" v-model="tempDevice.devBrand" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="类型/属性" required>
          <el-input type="text" v-model="tempDevice.devType" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="使用位置" required>
          <el-input type="text" v-model="tempDevice.devPosition" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="设备情况" required>
          <el-select v-model="tempDevice.devSituation" placeholder="请选择" style="width:250px;">
            <el-option
              v-for="st in SituationType"
              :key="st.value"
              :label="st.label"
              :value="st.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" required>
          <el-input type="text" v-model="tempDevice.createTime" :disabled="true" style="width: 250px;">
          </el-input>
        </el-form-item>
        <el-form-item label="维修人员" required>
          <el-input type="text" v-model="tempDevice.userName" style="width: 250px;">
          </el-input>
          <p style="color:#848484;">说明:红色权限为对应菜单内的必选权限</p>
        </el-form-item>  
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateDevice()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {mapGetters} from 'vuex'

  export default {
    data() {
      return {
        search: '',
        totalCount: 0, //分页组件--数据总条数
        list: [],//表格的数据
        listLoading: false,//数据加载等待动画
        listQuery: {
          pageNum: 1, //页码
          pageRow: 50, //每页条数
        },
        dialogStatus: 'update',
        dialogFormVisible: false,
        textMap: {
          update: '编辑'
        },
        SituationType:[{
          value: '空闲中',
          label: '空闲中'
        },{
          value: '使用中',
          label: '使用中'
        },{
          value: '维护中',
          label: '维护中'
        },{
          value: '停用中',
          label: '停用中'
        }],
        tempDevice: {
          devId: '',
          devModel: '',
          devName: '',
          devBrand: '',
          devType: '',
          devPosition: '',
          devSituation: '',
          createTime: '',
          userName: '',
          permissions: [],
        }
      }
    },
    created() {
      this.getList();
      // if (this.hasPerm('devices:update')){
      //   this.getAllDevices();
      // }
    },
    computed: {
      // 模糊搜索
      lists () {
        const search = this.search
        if (search) {
          // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
          // 注意： filter() 不会对空数组进行检测。
          // 注意： filter() 不会改变原始数组。
          return this.list.filter(data => {
            // some() 方法用于检测数组中的元素是否满足指定条件;
            // some() 方法会依次执行数组的每个元素：
            // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
            // 如果没有满足条件的元素，则返回false。
            // 注意： some() 不会对空数组进行检测。
            // 注意： some() 不会改变原始数组。
            return Object.keys(data).some(key => {
              // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
              // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
              return String(data[key]).toLowerCase().indexOf(search) > -1
            })
          })
        }
        return this.list
      }
    },
    methods: {
      getList() {
        //查询列表
        this.listLoading = true;
        this.api({
          url: "/devices/listDevices",
          method: "get",
          params: this.listQuery
        }).then(data => {
          this.listLoading = false;
          this.list = data.list;
          this.totalCount = data.totalCount;
        })
      },
      handleSizeChange(val) {
        //改变每页数量
        this.listQuery.pageRow = val
        this.handleFilter();
      },
      handleCurrentChange(val) {
        //改变页码
        this.listQuery.pageNum = val
        this.getList();
      },
      handleFilter() {
        //查询事件
        this.listQuery.pageNum = 1
        this.getList()
      },
      getIndex($index) {
        //表格序号
        return (this.listQuery.pageNum - 1) * this.listQuery.pageRow + $index + 1
      },
      showUpdate($index) {    
        let device = this.list[$index];
        this.tempDevice.devModel = device.devModel;
        this.tempDevice.devId = device.devId;
        this.tempDevice.devName = device.devName;
        this.tempDevice.devBrand = device.devBrand;
        this.tempDevice.devType = device.devType;
        this.tempDevice.devPosition = device.devPosition;
        this.tempDevice.devSituation = device.devSituation;
        this.tempDevice.createTime = device.createTime;
        this.tempDevice.userName = device.userName;
        this.dialogStatus = "update"
        this.dialogFormVisible = true
      },
      updateDevice() {
        //修改用户信息
        let _vue = this;
        this.api({
          url: "/devices/updateDevices",
          method: "post",
          data: this.tempDevice
        }).then(() => {
          let msg = "修改成功";
          this.dialogFormVisible = false
          if (this.devId === this.tempDevice.devId && this.devModel === this.tempDevice.devModel) {
            msg = '修改成功,部分信息重新登录后生效'
          }
          this.$message({
            message: msg,
            type: 'success',
            duration: 1 * 1000,
            onClose: () => {
              _vue.getList();
            }
          })
        })
      },
    }
  }
</script>
<style scoped>
  .requiredPerm {
    color: #ff0e13;
  }
</style>
